package liuyao.webutils.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class TestAop {

    @Around("@annotation(liuyao.webutils.utils.anno.ThreadCache)")
    public Object around(ProceedingJoinPoint pjp) {
        Object[] args = pjp.getArgs();
        Object result = null;
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        boolean needCache = methodSignature.getReturnType() != void.class;
        if (needCache) {
            Class[] types = methodSignature.getParameterTypes();
            StringBuilder sb = new StringBuilder(methodSignature.getDeclaringType().getName())
                    .append("#").append(methodSignature.getName()).append(":");
            if (null != args) for (int i = 0; i < args.length; i++)
                sb.append(isBasic(types[i]) && null != args[i] ? args[i] : types[i].getName()).append(",");
            String cacheKey = sb.toString();

        }
        try {
            result = pjp.proceed(args);
            if (needCache) {

            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return result;
    }

    public static boolean isBasic(Class clz) {
        return clz == byte.class
                || clz == short.class
                || clz == int.class
                || clz == long.class
                || clz == float.class
                || clz == double.class
                || clz == char.class
                || clz == boolean.class
                || clz == Byte.class
                || clz == Short.class
                || clz == Integer.class
                || clz == Long.class
                || clz == Float.class
                || clz == Double.class
                || clz == Character.class
                || clz == Boolean.class
                || clz == String.class;
    }
}
